﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;


namespace iTouch.FoodSupervise.Common
{
    /// <summary>
    /// WebMessageBox 的摘要说明
    /// </summary>
    public class WebMessageBox
    {
        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(System.Web.UI.Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string msg)
        {
            //Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
        {
            //Response.Write("<script>alert('帐户审核通过！现在去为企业充值。');window.location=\"" + pageurl + "\"</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');window.location=\"" + url + "\"</script>");


        }
        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirects(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());

        }

        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");

        }

        /// <summary>
        /// 网页消息对话框
        /// </summary>
        /// <param name="Message">要显示的消息文本</param>
        public static void Show(string Message)
        {
            HttpContext.Current.Response.Write("<script language='javascript' type='text/javascript'>alert('" + Message + "')</script>");
            HttpContext.Current.Response.Write("<script>history.go(-1)</script>");
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 网页消息对话框
        /// </summary>
        /// <param name="Message">要显示的消息文本</param>
        /// <param name="Src">点击确定后跳转的页面</param>
        public static void Show(string Message, string Src)
        {
            HttpContext.Current.Response.Write("<script language='javascript' type='text/javascript'>alert('" + Message + "');location.href='" + Src + "'</script>");
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 网页消息对话框
        /// </summary>
        /// <param name="Message">要显示的消息文本</param>
        /// <param name="Close">关闭当前页面</param>
        public static void Show(string Message, bool Close)
        {
            if (Close)
            {
                HttpContext.Current.Response.Write("<script language='javascript' type='text/javascript'>alert('" + Message + "');window.close()</script>");
                HttpContext.Current.Response.End();
            }
            else
            {
                HttpContext.Current.Response.Write("<script language='javascript' type='text/javascript'>alert('" + Message + "')</script>");
                HttpContext.Current.Response.End();
            }
        }

        /// <summary>
        /// 后台文本框输入危险字符报错
        /// </summary>
        public static void InputWorry()
        {
            HttpContext.Current.Response.Write("{\"statusCode\":\"300\", \"message\":\"操作失败，请尽量不要输入特殊字符\"," +
                 " \"navTabId\":\"\", \"rel\":\"\",\"callbackType\":" +
                 "\"\",\"forwardUrl\":\"\"}");
        }

        /// <summary>
        /// 错误成功消息固定
        /// </summary>
        /// <param name="isRight">true：返回成功，false：返回失败</param>
        /// <param name="closeCurrent">true：成功之后关闭当前窗口</param>
        /// <param name="navTabId">目标页面ID</param>
        /// <param name="rel">目标页面ID</param>
        /// <param name="forwardUrl">成功之后需要刷新的页面</param>
        public static void DwzMessage(bool isRight, bool closeCurrent, string navTabId, string rel, string forwardUrl)
        {
            if (isRight == false)
            {
                HttpContext.Current.Response.Write("{\"statusCode\":\"300\", \"message\":\"操作失败\"," +
                 " \"navTabId\":\"\", \"rel\":\"\",\"callbackType\":" +
                 "\"\",\"forwardUrl\":\"\"}");
            }
            else
            {
                string close = closeCurrent ? "closeCurrent" : "";
                HttpContext.Current.Response.Write("{\"statusCode\":\"200\", \"message\":\"操作成功\"," +
                                                   " \"navTabId\":\"" + navTabId + "\", \"rel\":\"" + rel + "\",\"callbackType\":" +
                                                   "\"" + close + "\",\"forwardUrl\":\"" + forwardUrl + "\"}");
            }
        }
        /// <summary>
        /// 错误成功消息固定，新增code
        /// </summary>
        /// <param name="isRight">true：返回成功，false：返回失败</param>
        /// <param name="closeCurrent">true：成功之后关闭当前窗口</param>
        /// <param name="navTabId">目标页面ID</param>
        /// <param name="rel">目标页面ID</param>
        /// <param name="forwardUrl">成功之后需要刷新的页面</param>
        /// <param name="code">不清楚搞什么用</param>
        public static void DwzMessage(bool isRight, bool closeCurrent, string navTabId, string rel, string forwardUrl,int code)
        {
            if (isRight == false)
            {
                HttpContext.Current.Response.Write("{\"statusCode\":\"300\",\"Code\":\"\", \"message\":\"操作失败\"," +
                 " \"navTabId\":\"\", \"rel\":\"\",\"callbackType\":" +
                 "\"\",\"forwardUrl\":\"\"}");
            }
            else
            {
                string close = closeCurrent ? "closeCurrent" : "";
                HttpContext.Current.Response.Write("{\"statusCode\":\"200\", \"Code\":\""+code+"\",\"message\":\"操作成功\"," +
                                                   " \"navTabId\":\"" + navTabId + "\", \"rel\":\"" + rel + "\",\"callbackType\":" +
                                                   "\"" + close + "\",\"forwardUrl\":\"" + forwardUrl + "\"}");
            }
        }
        /// <summary>
        /// 登陆过期窗口使用
        /// </summary>
        /// <param name="isRight">true：返回成功，false：返回失败</param>
        /// <param name="msg">错误消息</param>
        public static void DwzMessage(bool isRight, string msg)
        {
            if (isRight == false)
            {
                HttpContext.Current.Response.Write("{\"statusCode\":\"300\", \"message\":\"" + msg + "\"," +
                 " \"navTabId\":\"\", \"rel\":\"\",\"callbackType\":" +
                 "\"\",\"forwardUrl\":\"\"}");
            }
            else
            {
                HttpContext.Current.Response.Write("{\"statusCode\":\"200\", \"message\":\"操作成功\"," +
                                                   " \"navTabId\":\"" + 1 + "\", \"title\":\"\",\"rel\":\"" + 1 + "\",\"callbackType\":" +
                                                   "\"\",\"forwardUrl\":\"\"}");
            }
        }

        /// <summary>
        /// 自定义错误、成功消息
        /// </summary>
        /// <param name="isRight">true：返回成功，false：返回失败</param>
        /// <param name="closeCurrent">true：成功之后关闭当前窗口</param>
        /// <param name="navTabId">目标页面ID</param>
        /// <param name="rel">目标页面ID</param>
        /// <param name="forwardUrl">成功之后需要刷新的页面</param>
        /// <param name="trueMsg">自定义成功消息</param>
        /// <param name="falseMsg">自定义错误消息</param>
        public static void DwzMessage(bool isRight, bool closeCurrent, string navTabId, string rel, string forwardUrl, string trueMsg, string falseMsg)
        {
            if (isRight == false)
            {
                HttpContext.Current.Response.Write("{\"statusCode\":\"300\", \"message\":\"" + falseMsg + "\"," +
                 " \"navTabId\":\"\", \"rel\":\"\",\"callbackType\":" +
                 "\"\",\"forwardUrl\":\"\"}");
            }
            else
            {
                string close = closeCurrent ? "closeCurrent" : "";
                HttpContext.Current.Response.Write("{\"statusCode\":\"200\", \"message\":\"" + trueMsg + "\"," +
                                                   " \"navTabId\":\"" + navTabId + "\", \"rel\":\"" + rel + "\",\"callbackType\":" +
                                                   "\"" + close + "\",\"forwardUrl\":\"" + forwardUrl + "\"}");
            }
        }


        /// <summary>
        /// 可自定义错误消息，成功消息固定
        /// </summary>
        /// <param name="isRight">true：返回成功，false：返回失败</param>
        /// <param name="closeCurrent">true：成功之后关闭当前窗口</param>
        /// <param name="navTabId">目标页面ID</param>
        /// <param name="rel">目标页面ID</param>
        /// <param name="forwardUrl">成功之后需要刷新的页面</param>
        /// <param name="errorMsg">错误消息</param>
        public static void DwzMessage(bool isRight, bool closeCurrent, string navTabId, string rel, string forwardUrl, string errorMsg)
        {
            if (isRight == false)
            {
                HttpContext.Current.Response.Write("{\"statusCode\":\"300\", \"message\":\"" + errorMsg + "\"," +
                 " \"navTabId\":\"\", \"rel\":\"\",\"callbackType\":" +
                 "\"\",\"forwardUrl\":\"\"}");
            }
            else
            {
                string close = closeCurrent ? "closeCurrent" : "";
                HttpContext.Current.Response.Write("{\"statusCode\":\"200\", \"message\":\"操作成功\"," +
                                                   " \"navTabId\":\"" + navTabId + "\", \"rel\":\"" + rel + "\",\"callbackType\":" +
                                                   "\"" + close + "\",\"forwardUrl\":\"" + forwardUrl + "\"}");
            }
        }
    }
}